package yxy.game.pm2.command.union.leader;

import org.joda.time.DateTime;
import yxy.cherry.data.script.fomula;
import yxy.cherry.data.string.GameString;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.AreaGroupBean;
import yxy.game.pm2.bean.cluster.ClusterBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.NotificationHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.rank.RankActiveUnion;
import yxy.game.pm2.module.union.rank.RankAreaActiveUnion;
import yxy.game.pm2.module.union.rank.RankAreaGroupActiveUnion;

/** 联盟盟主 联盟发布招募广告 */
@MessageType(proto = Command.UnionRecruit, classtype = UnionLeaderMessage.UnionRecruitRequest.class)
public class CMD_UnionRecruit extends Command<UnionLeaderMessage.UnionRecruitRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionRecruitRequest request) {
		String playerCode = messageSource.getTag();

		UnionLeaderMessage.UnionRecruitResponse.Builder response = UnionLeaderMessage.UnionRecruitResponse.newBuilder();

		// 管理员权限
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnionManager(entry);
		if (!result.equals(Result.UnionManager)) {
			send(messageSource, response, result);
			;
			return;
		}
		UnionBean union = entry.getUnionBean();

		int costNumber = fomula.联盟招募花费元宝.intValue();
		Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNumber);
		try {
			new CurrencyHandle().cost(playerCode, CCReason.联盟管理, cost);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
			return;
		}

		// 发招募邮件
		new NotificationHandle().notifyChatInner(playerCode, GameString.联盟招募推送(union.getName(), union.getCode()));


		ServerBean serverBean = ServerBean.get(union.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
		AreaGroupBean areaGroupBean = AreaGroupBean.get(areaBean.getGroupId());
		ClusterBean clusterBean = ClusterBean.get(areaBean.getClusterId());

		int activeValue = fomula.联盟招募活跃时间.intValue();
		long active = DateTime.now().plusMinutes(activeValue).getMillis();
		new RankActiveUnion(clusterBean.getClusterId()).put(union.getCode(), active);
		RankAreaActiveUnion.get(union.getServerId()).put(union.getCode(), active);
		RankAreaGroupActiveUnion.get(areaGroupBean.getGroupId()).put(union.getCode(), active);

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionRecruitResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}